package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.constant.DubboMethodConstant;
import cc.rengu.igas.bomp.common.dao.InspectTaskDetailMapper;
import cc.rengu.igas.bomp.common.dao.ManagerInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.InspectTaskDetailMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.ManagerInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.InspectTaskDetail;
import cc.rengu.igas.bomp.common.entity.ManagerInfo;
import cc.rengu.igas.bomp.common.enums.InspectTypeEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.MchntInspectNoticeRequest;
import cc.rengu.igas.bomp.facade.response.MchntInspectNoticeResponse;
import cc.rengu.igas.channel.wechat.facade.request.InspectCheckMsgPushRequest;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhangxuran on 2020/03/30
 */
public class MchntInspectNoticeService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntInspectNoticeRequest mchntInspectNoticeRequest = new MchntInspectNoticeRequest();
        ConvertUtil.convertOutput(mchntInspectNoticeRequest);
        return mchntInspectNoticeRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse<MchntInspectNoticeResponse> bizResponse = new BizResponse<>();
        MchntInspectNoticeResponse mchntInspectNoticeResponse = new MchntInspectNoticeResponse();
        MchntInspectNoticeRequest mchntInspectNoticeRequest = (MchntInspectNoticeRequest) request;
        /*获取法人行号信息*/
        String instId = mchntInspectNoticeRequest.getHeader().getInstId();
        /*获取用户标识*/
        String userId = mchntInspectNoticeRequest.getUserId();
        /*获取巡检任务编号*/
        String inspectTaskNo = mchntInspectNoticeRequest.getInspectTaskNo();
        /*必填校验*/
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(inspectTaskNo)) {
            rglog.error("必填性校验失败!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /*交易通道标识获取*/
        List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList();
        List<ChannelCallCfg> msgDstchannelCallCfgList = channelCallCfgList.stream().filter(item -> item.getCallChannelId().equals("WXZF")).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(msgDstchannelCallCfgList)) {
            rglog.error("微信通道交易未配置！");
            throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
        }
        ChannelCallCfg channelCallCfg = msgDstchannelCallCfgList.get(0);
        /* 调用dobbu服务*/
        DubboService dubboService = new DubboServiceImpl();
        /*根据巡检任务号查询巡检任务详情列表*/
        InspectTaskDetailMapper inspectTaskDetailMapper = new InspectTaskDetailMapperImpl();
        ManagerInfoMapper managerInfoMapper = new ManagerInfoMapperImpl();
        List<InspectTaskDetail> inspectTaskDetailList = inspectTaskDetailMapper.selectInspectTaskDetailByInspectTaskNo(instId, inspectTaskNo);
        if (!CollectionUtils.isEmpty(inspectTaskDetailList)) {
            /*查询拓展经理信息*/
            for (InspectTaskDetail inspectTaskDetail : inspectTaskDetailList) {
                ManagerInfo managerInfo = managerInfoMapper.selectManagerInfoByPrimaryKey(instId, inspectTaskDetail.getInspectInstId(), inspectTaskDetail.getManagerId());
                if (null == managerInfo || "OPENID".equals(managerInfo.getOpenId())) {
                    rglog.error("拓展经理信息为空或者微信端未认证关联,instId:<{}>,mchntNo:<{}>,managerId:<{}>", instId, inspectTaskDetail.getMchntNo(), inspectTaskDetail.getManagerId());
                    continue;
                }

                /* 循环调用dobbu服务*/
                String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
                InspectCheckMsgPushRequest inspectCheckMsgPushRequest = new InspectCheckMsgPushRequest();
                /* 获取通道标识 */
                String channelId = channelCallCfg.getCallChannelId();
                /*报文头*/
                cc.rengu.igas.channel.wechat.facade.base.Header header = new cc.rengu.igas.channel.wechat.facade.base.Header();
                header.setInstId(instId);
                header.setTxnNum(channelCallCfg.getCallChannelTxn());
                header.setTraceNo(sysSeqNum + channelCallCfg.getCallIndex());
                header.setSrcSysId(AppParamConstant.SYS_ID);
                header.setChanlId(BompAppParamConstant.BOMP_SYS_ID);
                header.setBizType(channelCallCfg.getBizType());
                header.setTransTime(DateUtil.getCurrentTime());
                header.setTransDate(DateUtil.getCurrentDate());
                inspectCheckMsgPushRequest.setHeader(header);
                /*通道标识赋值*/
                inspectCheckMsgPushRequest.setChannelId(channelId);
                /*用户id赋值*/
                inspectCheckMsgPushRequest.setOpenId(managerInfo.getOpenId());
                /*巡检类型赋值*/
                String inspectType = inspectTaskDetail.getInspectType();
                if (InspectTypeEnum.PHONE_INSPECT.getInspectType().equals(inspectType)) {
                    inspectType = InspectTypeEnum.PHONE_INSPECT.getInspectDesc();
                } else if (InspectTypeEnum.ONLINE_INSPECT.getInspectType().equals(inspectType)) {
                    inspectType = InspectTypeEnum.ONLINE_INSPECT.getInspectDesc();
                } else if (InspectTypeEnum.OFFLINE_INSEPECT.getInspectType().equals(inspectType)) {
                    inspectType = InspectTypeEnum.OFFLINE_INSEPECT.getInspectDesc();
                }
                inspectCheckMsgPushRequest.setInspectType(inspectType);
                /*到期时间赋值*/
                inspectCheckMsgPushRequest.setInspectTime(inspectTaskDetail.getInspectBeginTime() + "至" + inspectTaskDetail.getInspectEndTime());
                /*商户号赋值*/
                inspectCheckMsgPushRequest.setMchntNo(inspectTaskDetail.getMchntNo());
                /*商户名称赋值*/
                inspectCheckMsgPushRequest.setMchntName(inspectTaskDetail.getMchntName());

                int iReturnCode = dubboService.asynCallDubboService(DubboMethodConstant.DUBBO_WECHAT_MSGPUSH_SERVICE, "inspectCheckMsgPush", inspectCheckMsgPushRequest, "DubboAsynBaseService");
                if (iReturnCode != 0) {
                    rglog.error("微信推送商户巡检核查通知(调用duboo)处理失败，instId:<{}>,mchntNo:<{}>,managerId:<{}>", instId, inspectTaskDetail.getMchntNo(), inspectTaskDetail.getManagerId());
                }
            }
        }
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntInspectNoticeResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntInspectNoticeResponse mchntInspectNoticeResponse = (MchntInspectNoticeResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntInspectNoticeResponse);
    }
}